package arithmetic.LeetCode._3Sum;

import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * @author zhangyanqi
 * @since 1.0 2017/10/20
 */
public class Solution {
    public List<List<Integer>> threeSum(int[] nums) {

        List<List<Integer>> result = new LinkedList<>();
        int length = nums.length;
        for (int i = 0; i < length - 2; i++) {
            int need = 0 - nums[i];
            for (int j = i + 1; j < length - 1; j++) {
                for (int k = j; j < length; j++) {
                    List<Integer> temp = new LinkedList<>();
                    temp.add(i);
                    temp.add(j);
                    temp.add(k);
                    temp.sort(new Comparator<Integer>() {
                        @Override
                        public int compare(Integer o1, Integer o2) {
                            if (o1 > o2) {
                                return 1;
                            }
                            if (o2 == o1) {
                                return 0;
                            }
                            if (o1 < o2) {
                                return -1;
                            }
                            return 0;
                        }
                    });
                    Iterator<List<Integer>> iterator = result.iterator();
                    boolean c = false;
                    while (iterator.hasNext()) {
                        List<Integer> next = iterator.next();
                        if (next.equals(temp)) {
                            c = true;
                            continue;
                        }
                    }
                    if (c) {
                        continue;
                    }
                    if (nums[i] + nums[j] == need) {
                        List<Integer> integers = new LinkedList<>();
                        integers.add(i);
                        integers.add(j);
                        integers.add(k);
                        integers.sort(new Comparator<Integer>() {
                            @Override
                            public int compare(Integer o1, Integer o2) {
                                if (o1 > o2) {
                                    return 1;
                                }
                                if (o2 == o1) {
                                    return 0;
                                }
                                if (o1 < o2) {
                                    return -1;
                                }
                                return 0;
                            }
                        });
                    }
                }

            }
        }
        return result;
    }

    public static void main(String[] args) {
        List<List<Integer>> lists = new Solution().threeSum(new int[]{1, -1});
        System.out.println(lists);
    }
}
